package com.relytosoft.mizar.media.camera.services.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.relytosoft.mizar.media.camera.api.entity.CameraBase;
import com.relytosoft.mizar.media.camera.api.entity.CameraChannel;
import com.relytosoft.mizar.media.camera.api.entity.CameraPlatform;
import com.relytosoft.mizar.media.camera.api.entity.CameraType;
import com.relytosoft.mizar.media.camera.services.service.*;
import com.relytosoft.mizar.media.common.constant.PropertyConstants;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lj
 * @date 2022/4/21 14:00
 */
@Service
@ConditionalOnProperty(value = PropertyConstants.PROPERTY_MIZAR_MEDIA_STORETYPE, havingValue = "HK")
public class HkSyncServiceImpl implements HkSyncService {

    @Resource
    private CameraPlatformService cameraPlatformService;

    @Resource
    private CameraBaseService cameraBaseService;

    @Resource
    private CameraChannelService cameraChannelService;

    @Resource
    private CameraTypeService cameraTypeService;

    @Override
    public Map<String, Integer> findCameraType() {
        List<CameraType> list = cameraTypeService.list();
        return list.stream().collect(Collectors.toMap(CameraType::getName, CameraType::getId));
    }

    @Override
    public Map<String, Integer> findCameraCode() {
        return cameraBaseService.findCameraCode();
    }

    @Override
    public List<CameraPlatform> selectAllPlatformCode(Integer type, Integer status) {
        LambdaQueryWrapper<CameraPlatform> wrapper = Wrappers.lambdaQuery();
        if (type != null) {
            wrapper.eq(CameraPlatform::getType, type);
        }
        wrapper.eq(CameraPlatform::getStatus, status);
        return cameraPlatformService.list(wrapper);
    }

    @Override
    public boolean batchInsertCameraBase(List<CameraBase> cameraBaseList) {
        return cameraBaseService.saveBatch(cameraBaseList);
    }

    @Override
    public boolean batchInsertCameraPlatform(List<CameraPlatform> cameraPlatformList) {
        return cameraPlatformService.saveBatch(cameraPlatformList);
    }

    @Override
    public boolean batchInsertCameraChannel(List<CameraChannel> cameraChannelList) {
        return cameraChannelService.saveBatch(cameraChannelList);
    }

    @Override
    public List<CameraBase> batchSelectByCameraIds(Set<Integer> set) {
        List<CameraBase> cameraBaseList = cameraBaseService.listByIds(set);
        QueryWrapper<CameraPlatform> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(CameraPlatform::getOwnId, set);
        List<CameraPlatform> cameraPlatformList = cameraPlatformService.list(wrapper);
        Map<Integer, String> map = cameraPlatformList.stream().collect(Collectors.toMap(CameraPlatform::getOwnId, CameraPlatform::getPlatformCameraCode));
        for (CameraBase cameraBase : cameraBaseList) {
            cameraBase.setEncodeDevIndexCode(map.get(cameraBase.getId()));
        }
        return cameraBaseList;
    }

    @Override
    public List<CameraChannel> batchSelectByCameraChannelIds(Set<Integer> set) {
        List<CameraChannel> cameraChannelList = cameraChannelService.listByIds(set);
        QueryWrapper<CameraPlatform> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(CameraPlatform::getOwnId, set);
        List<CameraPlatform> cameraPlatformList = cameraPlatformService.list(wrapper);
        Map<Integer, String> map = cameraPlatformList.stream().collect(Collectors.toMap(CameraPlatform::getOwnId,
                CameraPlatform::getPlatformChannelCode));
        for (CameraChannel cameraChannel : cameraChannelList) {
            cameraChannel.setPlatformChannelCode(map.get(cameraChannel.getId()));
        }
        return cameraChannelList;
    }

    @Override
    public boolean batchUpdateCameraChannel(List<CameraChannel> cameraChannelList) {
        return cameraChannelService.saveOrUpdateBatch(cameraChannelList);
    }

    @Override
    public List<CameraBase> selectNeedSyncDevice() {

        //1.查询所有设备信息
        List<CameraBase> cameraBaseList = cameraBaseService.list();


        //2. 查询已经关联比特大陆的数据
        LambdaQueryWrapper<CameraPlatform> wrapper = new QueryWrapper<CameraPlatform>().lambda();
        wrapper.eq(CameraPlatform::getType, 1);
        wrapper.eq(CameraPlatform::getPlatformName, "BTDL");
        List<CameraPlatform> cameraPlatformNameList = cameraPlatformService.list(wrapper);

        //3. 关联数据
        if (cameraPlatformNameList.size() > 0) {
            for (CameraPlatform cameraPlatform : cameraPlatformNameList) {
                for (CameraBase cameraBase : cameraBaseList) {
                    if (cameraPlatform.getOwnId().equals(cameraBase.getId())) {
                        cameraBase.setPlatformCameraCode(cameraPlatform.getPlatformCameraCode());
                        break;
                    }
                }
            }
        }
        return cameraBaseList;
    }

    @Override
    public boolean batchUpdateCameraBase(List<CameraBase> cameraBaseList) {
        return cameraBaseService.saveOrUpdateBatch(cameraBaseList);
    }
}
